package com.pay.cloud.activity;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;

import com.pay.cloud.KeyInfo;
import com.pay.cloud.R;
import com.pay.cloud.fragment.ConnectFragment;
import com.pay.cloud.fragment.IpCableStaticFragment;
import com.pay.cloud.fragment.TipVerticalFragment;

import static com.pay.cloud.KeyInfo.KEY_CANCEL;
import static com.pay.cloud.KeyInfo.KEY_ENTER;
import static com.pay.cloud.KeyInfo.KEY_NUM_1;
import static com.pay.cloud.KeyInfo.KEY_NUM_2;
import static com.pay.cloud.KeyInfo.KEY_NUM_3;
import static com.pay.cloud.KeyInfo.KEY_NUM_4;
import static com.pay.cloud.KeyInfo.KEY_NUM_5;
import static com.pay.cloud.KeyInfo.KEY_NUM_6;
import static com.pay.cloud.KeyInfo.KEY_NUM_7;
import static com.pay.cloud.KeyInfo.KEY_NUM_8;
import static com.pay.cloud.fragment.TipFragment.TipType.SUCCESS;

public class ConnectActivity extends AbstractActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect);
        setMainFragment(new ConnectFragment());
    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {
        if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
            if (mainFragment instanceof ConnectFragment) {
                Intent intent = new Intent(this, SettingActivity.class);
                startActivity(intent);
            } else {
                setMainFragment(new ConnectFragment());
            }
        } else if (mainFragment instanceof ConnectFragment) {
            if (keyInfo == KEY_NUM_1) {
                enableWifi(true);
                enable4G(true);
                enableCable(true);
                setMainFragment(new TipVerticalFragment(SUCCESS, "已切换至自动连网"));
            } else if (keyInfo == KEY_NUM_2) {
                enableWifi(false);
                enable4G(false);
                enableCable(true);
                setMainFragment(new TipVerticalFragment(SUCCESS, "已切换至有线连网"));
            } else if (keyInfo == KEY_NUM_3) {
                enableWifi(true);
                enable4G(false);
                enableCable(false);
                setMainFragment(new TipVerticalFragment(SUCCESS, "已切换至WIFI连网"));
            } else if (keyInfo == KEY_NUM_4) {
                enableWifi(false);
                enable4G(true);
                enableCable(false);
                setMainFragment(new TipVerticalFragment(SUCCESS, "已切换至4G连网"));
            } else if (keyInfo == KEY_NUM_5) {
                dhcp();
                setMainFragment(new TipVerticalFragment(SUCCESS, "已切换至自动分配IP"));
            } else if (keyInfo == KEY_NUM_6) {
                setMainFragment(new IpCableStaticFragment());
            } else if (keyInfo == KEY_NUM_7) {
                Intent intent = new Intent(this, WifiActivity.class);
                startActivity(intent);
            } else if (keyInfo == KEY_NUM_8) {
                Intent intent = new Intent(this, DomainActivity.class);
                startActivity(intent);
            }
        }

        return false;
    }


    private void enableWifi(boolean bool) {
        WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(bool);
    }

    private void enable4G(boolean bool) {
        if (bool) {
            Intent i = new Intent("com.snstar.networkparameters.DATA_OPEN");
            sendBroadcast(i);
        } else {
            Intent i = new Intent("com.snstar.networkparameters.DATA_CLOSE");
            sendBroadcast(i);
        }
    }

    private void enableCable(boolean bool) {
        if (bool) {
            Intent i = new Intent("com.snstar.networkparameters.ETH_OPEN");
            sendBroadcast(i);
        } else {
            Intent i = new Intent("com.snstar.networkparameters.ETH_CLOSE");
            sendBroadcast(i);
        }
    }

    private void dhcp() {
        Intent i = new Intent("com.snstar.networkparameters.ETH_DHCP");
        sendBroadcast(i);
    }
}
